package org.springblade.modules.admin.task.feishu;

import cn.hutool.core.util.StrUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.enums.FeiShuDocumentEnum;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.feishu.FeiShuBatchDTO;
import org.springblade.modules.admin.dto.feishu.FeiShuFieldsDTO;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.SignStatusEnum;
import org.springblade.modules.admin.service.IElevatorContractService;
import org.springblade.modules.admin.service.feishu.IFeiShuDocumentService;
import org.springblade.modules.admin.vo.feishu.LarkUserSignDetailVO;
import org.springblade.modules.admin.vo.feishu.MaintainElevatorContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 宇航的排期电梯合同飞书多维表格
 * @author Sharry
 * @since 2024-08-06
 */
@Component
@Slf4j
public class LarkMaintainElevatorContractJob {

	private IFeiShuDocumentService feiShuDocumentService;

	private IElevatorContractService elevatorContractService;

	@Autowired
	public void setFeiShuDocumentService(IFeiShuDocumentService feiShuDocumentService) {
		this.feiShuDocumentService = feiShuDocumentService;
	}

	@Autowired
	public void setElevatorContractService(IElevatorContractService elevatorContractService) {
		this.elevatorContractService = elevatorContractService;
	}

	@XxlJob("galaxyScheduledMaintainElevatorContractJob")
	public void galaxyScheduledMaintainElevatorContractJob() {
		XxlJobHelper.handleSuccess("定时任务更新宇航的飞书已排期电梯合同数据表-[执行]开始");
		try {
			// 1. 数据准备：appToken，tableId，要对比新增的数据
			String appToken = FeiShuDocumentEnum.SCHEDULED_MAINTAIN_ELEVATOR_CONTRACT.getAppToken();
			String tableId = FeiShuDocumentEnum.SCHEDULED_MAINTAIN_ELEVATOR_CONTRACT.getTableId();
			List<MaintainElevatorContractVO> targetList = elevatorContractService.getLarkMaintainElevatorContractList();
			// 2. 数据处理、数据封装
			FeiShuBatchDTO batchDTO = new FeiShuBatchDTO();
			List<FeiShuFieldsDTO> records = new ArrayList<>();
			HashMap<Integer, String> payDateDescMap = new HashMap<Integer, String>(10);
			payDateDescMap.put(1, "月结");
			payDateDescMap.put(2, "两月结");
			payDateDescMap.put(3, "季度结");
			payDateDescMap.put(4, "四月结");
			payDateDescMap.put(5, "半年结");
			payDateDescMap.put(6, "年度结");
			payDateDescMap.put(7, "50%,50%");
			payDateDescMap.put(8, "预付50%,完结50%");
			payDateDescMap.put(9, "一次性付款");
			payDateDescMap.put(10, "免费");
			HashMap<Integer, String> invoiceTypeMap = new HashMap<Integer, String>(8);
			invoiceTypeMap.put(1, "增值税专用发票");
			invoiceTypeMap.put(2, "增值税普通发票");
			invoiceTypeMap.put(3, "收据");
			invoiceTypeMap.put(4, "已收款未开票");
			invoiceTypeMap.put(5, "已开收据为付款");
			invoiceTypeMap.put(6, "已开收据未开发票");
			invoiceTypeMap.put(7, "预支发票");
			invoiceTypeMap.put(8, "其他");

			for (MaintainElevatorContractVO m : targetList) {
				FeiShuFieldsDTO element = new FeiShuFieldsDTO();
				Map<String, Object> elementMap = new HashMap<>(30);
				elementMap.put("合同ID", m.getContractId());
				elementMap.put("维保合同编号", m.getContractNo());
				if(!Func.isNull(m.getContractCreateTime())){
					elementMap.put("合同创建时间", m.getContractCreateTime().getTime());
				}
				if(!Func.isNull(m.getFirstMaintainTime())){
					elementMap.put("首次保养时间",m.getFirstMaintainTime().getTime());
				}
				if(!Func.isNull(m.getElevatorMonthCost())){
					Double costDouble = Double.valueOf(m.getElevatorMonthCost());
					elementMap.put("单梯费用", costDouble);
				}
				elementMap.put("梯数",m.getElevatorCounts());
				elementMap.put("合同总价",m.getContractTotal());
				// 票据类型
				if(!Func.isNull(m.getPayType())){
					elementMap.put("票据类型",invoiceTypeMap.get(m.getPayType()));
				}
				// 付款周期
				if(!Func.isNull(m.getPayDateDesc())){
					elementMap.put("付款周期",payDateDescMap.get(m.getPayDateDesc()));
				}
				elementMap.put("项目名称",m.getBuildingName());
				if(!Func.isNull(m.getContractBeginTime())){
					elementMap.put("合同开始时间",m.getContractBeginTime().getTime());
				}
				if(!Func.isNull(m.getContractEndTime())){
					elementMap.put("合同结束时间",m.getContractEndTime().getTime());
				}
				element.setFields(elementMap);
				records.add(element);
			}
			batchDTO.setRecords(records);
			// 3. 调用飞书API更新数据表、发送消息通知
			String finalResult = feiShuDocumentService.commandCompareAddBatch(appToken, tableId, batchDTO, "合同ID");
			String message = "定时器触发更新" + FeiShuDocumentEnum.getDescription(appToken, tableId) + "结果：" + finalResult;
			feiShuDocumentService.commandSendGroupMessage(appToken, tableId, message);
			XxlJobHelper.handleSuccess("定时任务更新宇航的飞书已排期电梯合同数据表-[执行]成功");
		} catch (Exception e) {
			XxlJobHelper.handleSuccess("定时任务更新宇航的飞书已排期电梯合同数据表-[执行]失败" + e);
		}
	}
}
